package com.calsoft.Hops_Mobile_Tablet;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.calsoft.Hops_Mobile_Tablet.component.SimpleAlertDialogFragment;
import com.calsoft.Hops_Mobile_Tablet.util.SessionManager;


/*
 * User: Minh Diep
 * Date: 6/25/13
 * Time: 6:37 PM
 */

public class LoginActivity extends FragmentActivity implements SimpleAlertDialogFragment.AlertDialogListener {

    private EditText emailEditText, passwordEditText;
    private Button loginBtn;
    private SessionManager sessionManager;
    private SimpleAlertDialogFragment dialogFragment;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        sessionManager = new SessionManager(getApplicationContext());
        emailEditText = (EditText) findViewById(R.id.user_email_edit);
        passwordEditText = (EditText) findViewById(R.id.password_edit);
        loginBtn = (Button) findViewById(R.id.login_btn);

        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userEmail = emailEditText.getText().toString();
                String password = passwordEditText.getText().toString();

                if(userEmail.trim().length() > 0 && password.trim().length() > 0){
                    if(userEmail.equals("40113000217") && password.equals("test")){
                        sessionManager.createLoginSession(userEmail);
                        // Staring MainActivity
                        Intent i = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(i);
                        finish();
                    }else{
                        dialogFragment = new SimpleAlertDialogFragment("Login Failed", "User's Email and Password don't match");
                        dialogFragment.show(getSupportFragmentManager(), "SimpleAlertDialogFragment");
                    }
                }else{
                    dialogFragment = new SimpleAlertDialogFragment("Login Failed", "Please enter User's Email and Password");
                    dialogFragment.show(getSupportFragmentManager(), "SimpleAlertDialogFragment");
                }
            }
        });

    }

    @Override
    public void onDialogPositiveClick(DialogFragment dialog) {
        dialogFragment.dismiss();
    }
}